package com.fdb.efp.console.api.rest;

import java.util.List;
import java.util.Map;

import com.fdb.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fdb.efp.console.service.facade.SResourceService;
import com.fdb.efp.console.service.vo.SRescActVO;
import com.fdb.efp.console.service.vo.SResourceVO;
import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import com.fdb.basic.framework.core.constant.ApplicationConstance;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class SResourceController extends AbstractController  {
	
	@Autowired
	@Qualifier("sResourceService")
	private SResourceService sResourceService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/s/resources", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SResourceVO>> querySResourceAll(
			@RequestBody SResourceVO vo) {		
		// 根据用户角色获取用户权限方案
		List<SResourceVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);

		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sResourceService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = sResourceService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = sResourceService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = sResourceService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/resource/{rescCode}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SResourceVO> queryByPk(@PathVariable("rescCode") String rescCode) {		
		SResourceVO inVo = new SResourceVO();
				inVo.setRescCode(rescCode);
		SResourceVO outputVo = sResourceService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param sResource
	 * @return
	 */
	@RequestMapping(value="/s/resource", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SResourceVO inSResourceVo) {		
		int outputVo = sResourceService.deleteByPk(inSResourceVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inSResourceVo
	 * @return
	 */
	@RequestMapping(value="/s/resource", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SResourceVO inSResourceVo) {		
		setUserInfoToVO(inSResourceVo);
		int outputVo = sResourceService.updateByPk(inSResourceVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inSResourceVo
	 * @return
	 */
	@RequestMapping(value="/s/resource", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSResource(@RequestBody SResourceVO inSResourceVo) {
		setUserInfoToVO(inSResourceVo);
		int outputVo = sResourceService.insertSResource(inSResourceVo);
		if ( outputVo == -2) {
			ResponseData<Integer> responseData = new ResponseData<Integer>();
			responseData.setRows(outputVo);
			responseData.setMessage("资源代码已存在,新增失败!");
			responseData.setCode(ApplicationConstance.FAILURE);
			responseData.setSuccess(false);
			return responseData;
		}else
			return getResponseData(outputVo);
		
	}
	
	@RequestMapping(value="/s/queryChildrens", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SResourceVO>> queryChildrens(@RequestBody SResourceVO inSResourceVo) {
		List<SResourceVO> sResourceVOs = sResourceService.queryChildrens(inSResourceVo);
		return getResponseData(sResourceVOs);
	}
	
	@RequestMapping(value="/s/queryRescAll", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SResourceVO>> queryRescAll(@RequestBody SResourceVO inSResourceVo) {
		List<SResourceVO> sResourceVOs = sResourceService.queryRescAll(inSResourceVo);
		return getResponseData(sResourceVOs);
	}
	
	/**
	 * 根据主键删除信息
	 * @param sRescAct
	 * @return
	 */
	@RequestMapping(value="/s/resc/act", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SRescActVO inSRescActVo) {		
		int outputVo = sResourceService.deleteByPk(inSRescActVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inSRescActVo
	 * @return
	 */
	@RequestMapping(value="/s/resc/act", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SRescActVO inSRescActVo) {	
		setUserInfoToVO(inSRescActVo);
		int outputVo = sResourceService.updateByPk(inSRescActVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inSRescActVo
	 * @return
	 */
	@RequestMapping(value="/s/resc/act", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSRescAct(@RequestBody SRescActVO inSRescActVo) {
		setUserInfoToVO(inSRescActVo);
		int outputVo = sResourceService.insertSRescAct(inSRescActVo);
		return getResponseData(outputVo);
		
	}
	
	@RequestMapping(value="/s/resc/rescActs", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SRescActVO>> queryAllByRescCode(
			@RequestBody SResourceVO inSResourceVO) {	
		String rescCode = inSResourceVO.getRescCode();
		List<SRescActVO> outputVo = sResourceService.queryAllByRescCode(rescCode);
		return getResponseData(outputVo);
		
	}
	
	@RequestMapping(value="/s/user/resc/data", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SResourceVO>> queryUserRescData() {
		SResourceVO res = new SResourceVO();
		setUserInfoToVO(res);
		String userCode = res.getLoginUserId();
		List<SResourceVO> sResourceVOs = sResourceService.queryUserRescData(userCode);
		return getResponseData(sResourceVOs);
	}
}
